/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.sdp;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ListIterator;
import javamail.sdp.util.codecInfo;
import javamail.sdp.util.eRtpStaticPayload;
import javamail.sdp.util.eSdpMediaType;
import javamail.sdp.util.mediaLine;

/**
 *
 * @author sriramo=- 12334 12334 IN IP4 172.31.212.23
 */
public class sdpGenerator {
    static String selfAddress = null;
    public static void createSdp(sdpMessage l, sdpMessage r, int sessId) {
        try {
            selfAddress = InetAddress.getLocalHost().toString();
            String[] address = selfAddress.split("/");
            if(address.length>1)
                selfAddress = address[1];
        } catch (UnknownHostException e) {
            selfAddress = "127.0.0.1";
        } catch (Exception e) {
            selfAddress = "localhost";
        }
        mediaLine lclMedLine = null;
        codecInfo lclCdcLine = null;
        
        if(l==null)
            return;
        
        l.version = r.version;
        l.session = r.session;
        
        //if(r.getSessionId() != null)
          //  l.originator = "- "+ r.getSessionId() + " " + r.getSessionId() + " IN IP4 " + selfAddress;
        //else
            l.originator = "- "+ sessId + " " + sessId + " IN IP4 " + selfAddress;
        
        l.connect = "IN IP4 "+selfAddress;
        l.time = r.time;
        ListIterator rmtAttrList = r.commAttr.listIterator();
        // We are only going to send information.
        //l.commAttr.add("sendrecv");
        //while(rmtAttrList.hasNext())
        //    l.commAttr.add(rmtAttrList.next());
        ListIterator rmtMediaList = r.mediaList.listIterator();
        ListIterator rmtCdcLst = null;
        mediaLine rmtMedLine = null;
        codecInfo rmtCdcLine = null;
        while(rmtMediaList.hasNext()) {
            rmtMedLine = (mediaLine)rmtMediaList.next();
            lclMedLine = new mediaLine();
            lclMedLine.media = rmtMedLine.media;
            // Support only audio only now. For other media set port as zero
            if(rmtMedLine.media == eSdpMediaType.AUDIO)
                lclMedLine.port = rmtMedLine.port+2;
            else {
                lclMedLine.port = 0;
                lclCdcLine = new codecInfo();
                lclCdcLine.payloadNumber = 0;
            }
            lclMedLine.profile = rmtMedLine.profile;
            rmtCdcLst = rmtMedLine.codecList.listIterator();
            while(rmtCdcLst.hasNext()) {
                rmtCdcLine = (codecInfo)rmtCdcLst.next();
                if(!eRtpStaticPayload.fromPayload(rmtCdcLine.payloadNumber).equals(eRtpStaticPayload.NIL)) {
                    lclCdcLine = new codecInfo();
                    lclCdcLine.payloadNumber = rmtCdcLine.payloadNumber;
                    lclCdcLine.rtpmap = rmtCdcLine.rtpmap;
                    lclCdcLine.fmtp = rmtCdcLine.fmtp;
                    lclMedLine.codecList.add(lclCdcLine);
                }
            }
            l.mediaList.add(lclMedLine);
        }
    }

}
